package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.enums.ArticleReplyStatus;
import com.example.forum.common.enums.ResultCode;
import com.example.forum.common.validation.ValidationUtils;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.ArticleReply;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hanzishuai
 * @date 2025/4/12 11:46
 * @Description
 */
@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService articleService;

    @Override
    @Transactional
    public void creat(ArticleReply articleReply) {
        // 校验
        if (articleReply == null || articleReply.getArticleId() == null
                || articleReply.getPostUserId() == null
                || !StringUtils.hasLength(articleReply.getContent())) {
            log.warn("{}", ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 设置默认值
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(0);
        articleReply.setState(ArticleReplyStatus.NOT_BANNED.getStatus());
        articleReply.setDeleteState(ArticleReplyStatus.NOT_DELETE.getStatus());
        LocalDateTime date = LocalDateTime.now();

        articleReply.setUpdateTime(date);
        articleReply.setCreateTime(date);

        // 写入数据库
        int row = articleReplyMapper.insertSelective(articleReply);

        // 校验
        ValidationUtils.singleRowAffected(row);

        // 更新贴子表中的回复数
        articleService.addOneReplyCountById(articleReply.getArticleId());

        log.info("回复帖子成功，article id = {}，user id = {}", articleReply.getArticleId(), articleReply.getPostUserId());

    }


    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        // 参数校验
        ValidationUtils.id(articleId, "帖子 Id");

        // 调用 DAO
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);


        return result;
    }

    @Override
    @Transactional
    public void delete(Long articleReplyId) {
        ValidationUtils.id(articleReplyId, "帖子回复 Id");
        ArticleReply articleReply = articleReplyMapper.selectByPrimaryKey(articleReplyId);
        ValidationUtils.exists(articleReply);

        ArticleReply updateArticleReply = new ArticleReply();
        updateArticleReply.setId(articleReplyId);
        updateArticleReply.setDeleteState(ArticleReplyStatus.IS_DELETE.getStatus());
        updateArticleReply.setUpdateTime(LocalDateTime.now());

        int row = articleReplyMapper.updateByPrimaryKeySelective(updateArticleReply);

        ValidationUtils.singleRowAffected(row);

        // 更新文章的回复数
        articleService.subOneReplyCountById(articleReply.getArticleId());

        log.info("删除回复帖子成功，article id = {}，user id = {}", articleReply.getArticleId(), articleReply.getPostUserId());

    }

    @Override
    public ArticleReply selectByArticleReplyId(Long articleReplyId) {
        ValidationUtils.id(articleReplyId, "帖子回复 Id");
        ArticleReply articleReply = articleReplyMapper.selectByPrimaryKey(articleReplyId);

        ValidationUtils.exists(articleReply);

        return articleReply;
    }


}



















